package com.ruoyi.questions.service.impl;

import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.questions.domain.Xuanze;
import com.ruoyi.questions.mapper.QuestionsMapper;
import com.ruoyi.questions.domain.Questions;
import com.ruoyi.questions.service.IQuestionsService;

/**
 * 题库管理Service业务层处理
 * 
 * @author shz
 * @date 2024-09-12
 */
@Service
public class QuestionsServiceImpl implements IQuestionsService 
{
    @Autowired
    private QuestionsMapper questionsMapper;

    /**
     * 查询题库管理
     * 
     * @param id 题库管理主键
     * @return 题库管理
     */
    @Override
    public Questions selectQuestionsById(Long id)
    {
        return questionsMapper.selectQuestionsById(id);
    }

    /**
     * 查询题库管理列表
     * 
     * @param questions 题库管理
     * @return 题库管理
     */
    @Override
    public List<Questions> selectQuestionsList(Questions questions)
    {
        return questionsMapper.selectQuestionsList(questions);
    }

    /**
     * 新增题库管理
     * 
     * @param questions 题库管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertQuestions(Questions questions)
    {
        questions.setCreateTime(DateUtils.getNowDate());
        int rows = questionsMapper.insertQuestions(questions);

        insertXuanze(questions);
        return rows;
    }

    /**
     * 修改题库管理
     * 
     * @param questions 题库管理
     * @return 结果
     */

    @Transactional
    @Override
    public int updateQuestions(Questions questions)
    {
        questions.setUpdateTime(DateUtils.getNowDate());
        String rights = questions.getRights();
        questionsMapper.deleteXuanzeByQid(questions.getId());
        insertXuanze(questions);
        List<Xuanze> xuanzeList = questions.getXuanzeList();
        List<Long> collect = xuanzeList.stream().map(Xuanze::getId).collect(Collectors.toList());
        String[] split = rights.split(",");
        List<String> res=new ArrayList<>();
        for(String x:split){
            if(!x.equals("")&&collect.contains(Long.valueOf(x))){
                res.add(x);
            }
        }

        String join = StrUtil.join(",", res);
        questions.setRights(join);
        return questionsMapper.updateQuestions(questions);
    }



    /**
     * 批量删除题库管理
     * 
     * @param ids 需要删除的题库管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteQuestionsByIds(Long[] ids)
    {
        questionsMapper.deleteXuanzeByQids(ids);
        return questionsMapper.deleteQuestionsByIds(ids);
    }

    /**
     * 删除题库管理信息
     * 
     * @param id 题库管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteQuestionsById(Long id)
    {
        questionsMapper.deleteXuanzeByQid(id);
        return questionsMapper.deleteQuestionsById(id);
    }

    /**
     * 新增选项信息
     * 
     * @param questions 题库管理对象
     */
    public void insertXuanze(Questions questions)
    {
        List<Xuanze> xuanzeList = questions.getXuanzeList();
        Long id = questions.getId();
        if (StringUtils.isNotNull(xuanzeList))
        {
            List<Xuanze> list = new ArrayList<Xuanze>();
            for (Xuanze xuanze : xuanzeList)
            {
                xuanze.setQid(id);
                list.add(xuanze);
            }
            if (list.size() > 0)
            {
                questionsMapper.batchXuanze(list);
            }
        }
    }
}
